package com.hzdl.apple.dao.imp.back;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.hzdl.apple.bean.Good;
import com.hzdl.apple.dao.BaseDaoImp;
import com.hzdl.apple.dao.back.BackGoodsDao;

public class BackGoodsDaoImp extends BaseDaoImp implements BackGoodsDao {

	@Override
	public boolean addGoods(Good good) {
		sql = "insert into goods values (?,?,?,?,?,?,?,?,?,?)";
		try {
			qr.update(sql,null,good.getGoodsName(),good.getGoodsColor(),good.getGoodsImgForShow(),good.getGoodsInfo(),good.getGoodsImg(),good.getIPadConn(),good.getMacDisc(),good.getPrice(),good.getNum());
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<Good> getAllGoods() {
		sql = "select * from goods";
		try {
			return qr.query(sql,new BeanListHandler<>(Good.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean deleteGoods(String GoodsID) {
		sql = "delete from goods where GoodsID = ?";
		try {
			qr.update(sql,GoodsID);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean updateGoods(String GoodsID, Good good) {
		sql = "update goods set GoodsName = ?,GoodsColor = ?,GoodsImgForShow = ?, GoodsInfo = ?,GoodsImg = ?,IpadConn = ?,MacDisc = ?,Price = ?,Num = ? where GoodsID = ?";
		try {
			qr.update(sql,good.getGoodsName(),good.getGoodsColor(),good.getGoodsImgForShow(),good.getGoodsInfo(),good.getGoodsImg(),good.getIPadConn(),good.getMacDisc(),good.getPrice(),good.getNum(),GoodsID);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int getGoodsNum() {
		sql = "select count(*) from goods";
		try {
			return qr.query(sql,new BeanHandler<>(Integer.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}
